from django.contrib import auth
from django.http import HttpResponse
from django.shortcuts import render, redirect
from app01.utils.random_code import random_code
from app01.utils.sub_comment import sub_comment_list
from app01.models import *
from app01.utils.pagination import Pagination
from django.db.models import F


# Create your views here.
def index(request):
    article_list = Articles.objects.filter(status=1).order_by('-change_date')
    category_list_first = article_list.filter(category=1)[:6]
    category_list_second = article_list.filter(category=2)[:6]

    query_params = request.GET.copy()
    # 分页
    pag = Pagination(
        current_page=int(request.GET.get('page', 1)),
        total_count=article_list.count(),
        base_url=request.path_info,
        params=query_params,
        # 每页显示的数量
        per_page_count=5,
        max_pager_count=5,
    )
    article_list = article_list[pag.start: pag.end]
    # 在线人数
    online_count = len(request.online_list)
    return render(request, "index.html", locals())


# 搜索文章
def search(request):
    search_key = request.GET.get('key', '')
    order = request.GET.get('order', '')
    tag = request.GET.get('tag', '')
    word = request.GET.getlist('word')
    query_params = request.GET.copy()
    article_list = Articles.objects.filter(title__icontains=search_key)

    if len(word) == 2:
        article_list = article_list.filter(word__range=word)
    if tag:
        article_list = article_list.filter(tag__title=tag)
    if order:
        try:
            article_list = article_list.order_by(order)
        except Exception:
            order = '-change_date'
    else:
        order = '-change_date'
    pag = Pagination(
        current_page=int(request.GET.get('page', 1)),
        total_count=article_list.count(),
        base_url=request.path_info,
        params=query_params,
        # 每页显示的数量
        per_page_count=1,
        max_pager_count=5,
    )
    article_list = article_list[pag.start: pag.end]
    return render(request, 'search.html', locals())


# 文章
def article(request, nid):
    # 找到文章的所有评论
    article_query = Articles.objects.filter(nid=nid)
    article_query.update(look_count=F('look_count') + 1)
    if not article_query:
        return redirect('/')
    article = article_query.first()
    comment_list = sub_comment_list(nid)
    if request.user.is_superuser:
        return render(request, "article.html", locals())
    if article.pwd:
        pwd = request.session.get(f'article_pwd__{nid}')
        if pwd == article.pwd:
            # 已经输入成功
            return render(request, "article.html", locals())
        return render(request, 'article_lock.html', locals())
    return render(request, "article.html", locals())


# 新闻
def news(request):
    return render(request, "news.html")


# 心情
def moods(request):
    # 查询所有的头像
    avatar_list = Avatars.objects.all()

    # 搜索
    key = request.GET.get('key', '')

    mood_list = Moods.objects.filter(content__contains=key).order_by('-create_date')
    query_params = request.GET.copy()
    # 分页器
    pag = Pagination(
        current_page=int(request.GET.get('page', 1)),
        total_count=mood_list.count(),
        base_url=request.path_info,
        params=query_params,
        # 每页显示的数量
        per_page_count=5,
        max_pager_count=7,
    )
    mood_list = mood_list[pag.start: pag.end]

    return render(request, 'moods.html', locals())


# 登陆
def login(request):
    return render(request, "login.html")


# 获取验证码
def get_random_code(request):
    data, valid_code = random_code()
    request.session['valid_code'] = valid_code
    request.session['error_count'] = 0
    return HttpResponse(data)


# 注册
def register(request):
    return render(request, "register.html")


# 注销退出
def logout(request):
    auth.logout(request)
    return redirect('/')


# 回忆录
def history(request):
    history_list = History.objects.all().order_by('-create_date')
    return render(request, "history.html", locals())


def about(request):
    return render(request, "about.html",locals())